﻿using BJYJ_Admin.Core.Utils;
using Furion.Logging;
using System.Reflection;

namespace BJYJ_Admin.Core;

/// <summary>
/// 数据库上下文对象
/// </summary>
public static class DbContext
{
    public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration)
    {
        // 获取所有数据库配置
        var dbOptionsxt = App.GetConfig<List<ConnectionConfig>>(path: "ConnectionConfigs");
        var firstConnectionConfig = dbOptionsxt.FirstOrDefault();
        // 如果多个数数据库传 List<ConnectionConfig>
        var configConnection = new ConnectionConfig()
        {
            DbType = SqlSugar.DbType.Dm,
            ConnectionString = firstConnectionConfig.ConnectionString,
            IsAutoCloseConnection = true,
        };

        SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
        db =>
        {
            // 单例参数配置，所有上下文生效
            db.Aop.OnLogExecuting = (sql, pars) =>
            {              
                Log.Information(sql);//输出sql
            };   
            

        });
        services.AddSingleton<ISqlSugarClient>(sqlSugar);   // 这边是SqlSugarScope用AddSingleton
    }

  
}
